/* $Id: UserEntry.java 138 2011-09-01 17:51:37Z altdotua@gmail.com $ */

package alt.djudge.frontend.server.datatypes;

import java.util.HashMap;
import java.util.Random;

import javax.cache.Cache;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import alt.djudge.frontend.server.models.CacheModel;
import alt.djudge.frontend.shared.dto.admin.AdminDTOEntry;
import alt.djudge.frontend.shared.dto.admin.UserEntryDTO;


@PersistenceCapable(detachable="true")
public class UserEntry extends AbstractEntry
{	
	private static final long serialVersionUID = 1L;
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	protected Long id;
	
	@Override
	public Long getId()
	{
		return id;
	}
	
	@Override
	public void setId(Long id)
	{
		this.id = id;
	}
	
	@Persistent
	private String password;
	
	@Persistent
	private String username;
	
	@Persistent
	private String hash;
	
	@Persistent
	private String salt;
	
	@Persistent
	private String email;
	
	@Persistent
	private String role = "user";
	
	@Persistent
	private Boolean virtual = false;
	
	@Persistent
	private Boolean loginAllowed = true;
	
	public void setValues(HashMap<String, Object> map)
	{
		active = (Boolean) map.get("active");
		password = (String) map.get("password");
		username = (String) map.get("username");
		role = (String) map.get("role");
		virtual = (Boolean) map.get("virtual");
	}

	
	public UserEntry()
	{
		// TODO Auto-generated constructor stub
	}
	
	public UserEntry(Long id)
	{
		this.id = id;
	}
	
	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getPassword()
	{
		return password;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getUsername()
	{
		return username;
	}

	public void setRole(String role)
	{
		this.role = role;
	}

	public String getRole()
	{
		return role;
	}
	
	public boolean isAdmin()
	{
		return "ADMIN".equalsIgnoreCase(role);
	}

	public boolean isBoard()
	{
		return "BOARD".equalsIgnoreCase(role);
	}

	@Override
	public String toString()
	{
		return username;
	}

	public static String getUserByUsernamePasswordCacheKey(String username, String password)
	{
		return UserEntry.class.getSimpleName() + "@uname=" + username + "@pwd=" + password;
	}
	
	public static String getUserByEmailCacheKey(String email)
	{
		return UserEntry.class.getSimpleName() + "@email=" + email;
	}
	
	@Override
	public void clearCacheCustom()
	{
		Cache cache = CacheModel.getStaticCache();
		//cache.remove(CacheModel.getUserEmailKey(email));
		cache.remove(CacheModel.getUserUsernameKey(username));
	}

	public void setVirtual(Boolean virtual)
	{
		this.virtual = virtual;
	}

	public Boolean getVirtual()
	{
		return virtual != null ? virtual : false;
	}


	public void generatePassword()
	{
		String chars = "qwertyuipasdfghjklzxcvbnm123456789".toUpperCase();
		StringBuilder sb = new StringBuilder();
		Random r = new Random();
		for (int i = 0; i < 6; i++)
			sb.append(chars.charAt(Math.abs(r.nextInt()) % chars.length()));
		setPassword(sb.toString());
	}


	public void setLoginAllowed(Boolean loginAllowed)
	{
		this.loginAllowed = loginAllowed;
	}


	public Boolean getLoginAllowed()
	{
		return loginAllowed != null ? loginAllowed : true;
	}
	
	public UserEntry(UserEntryDTO dtoEntry)
	{
		fromDTO(dtoEntry);
	}

	@Override
	public void fromDTO(AdminDTOEntry dtoEntry)
	{
		setUsername(dtoEntry.getFieldValue(1));
		setRole(dtoEntry.getFieldValue(2));		
	}
	
	@Override
	public UserEntryDTO ownAdminDTO()
	{
		String[] data = new String[] {
				getId().toString(),
				getUsername(),
				getRole()
		};
		UserEntryDTO entry = new UserEntryDTO(data);
		return entry;
	}

	public void setHash(String hash)
	{
		this.hash = hash;
	}
	
	public String getHash()
	{
		return hash;
	}

	public void setEmail(String email)
	{
		this.email = email;
	}
	
	public String getEmail()
	{
		return email;
	}

	public void setSalt(String salt)
	{
		this.salt = salt;
	}
	
	public String getSalt()
	{
		return salt;
	}
}
